package com.wy.sloth.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import lombok.Getter;
import lombok.Setter;

/**
 * 消息处理回执
 * 
 * @author wangyuan
 * @since 2019年3月18日
 */
@Getter
@Setter
public class SlothReceipt extends ArrayList<SlothReceiptData> implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = -846795766640065419L;
    
    /**
     * 消息回执状态：消息处理成功
     */
    public static final int STATUS_SUCCESS = 1;
    /**
     * 消息回执状态：消息处理失败
     */
    public static final int STATUS_ERROR = 0;
    
    /**
     * 检测是否存在错误信息
     * @return
     */
    public boolean hasErrors() {
        boolean result = false;
        Iterator<SlothReceiptData> iterator = iterator();
        while(iterator.hasNext()) {
            if(Integer.valueOf(STATUS_ERROR).equals(iterator.next().getStatus())){
                result = true;
                break;
            }
        }
        return result;
    }
    
    /**
     * 检测是否存在还未接收到的回执
     * @return
     */
    public boolean existUnanswered() {
        boolean result = false;
        Iterator<SlothReceiptData> iterator = iterator();
        while(iterator.hasNext()) {
            if(iterator.next().getStatus() == null){
                result = true;
                break;
            }
        }
        return result;
    }
    
    /**
     * 添加回执实例
     * @param receipt
     */
    public void addReceipt(SlothReceiptData receipt) {
        add(receipt);
    }
    
    /**
     * 添加回执实例
     * @param msgId
     * @param queueName
     */
    public void addReceipt(String msgId, String queueName) {
        add(new SlothReceiptData(msgId, queueName));
    }
    
    /**
     * 当确定回执只有一个时，可以使用此方法获取回执信息
     * @return
     */
    public SlothReceiptData getUniqueReceipt() {
        return get(0);
    }
    
    /**
     * 获取为失败状态的回执
     * @return
     */
    public List<SlothReceiptData> getErrorReceipt(){
        List<SlothReceiptData> errorReceipts = new ArrayList<>();
        Iterator<SlothReceiptData> iterator = iterator();
        SlothReceiptData tempInst = null;
        while(iterator.hasNext()) {
            tempInst = iterator.next();
            if(Integer.valueOf(STATUS_ERROR).equals(tempInst.getStatus())){
                errorReceipts.add(tempInst);
            }
        }
        return errorReceipts;
    }
    
    /**
     * 获取为失败状态的回执错误信息
     * @return
     */
    public String toErrorMsg(){
        StringBuilder sb = new StringBuilder();
        Iterator<SlothReceiptData> iterator = iterator();
        SlothReceiptData tempInst = null;
        while(iterator.hasNext()) {
            tempInst = iterator.next();
            if(Integer.valueOf(STATUS_ERROR).equals(tempInst.getStatus())){
                sb.append(tempInst.toString());
                sb.append("\\r\\n");
            }
        }
        return sb.toString();
    }
    
}
